/*
LocationChoose is a Jq-based js class,used as a location chooser;
usage:
var choose=new LocationChoose({self.container:"XX"});
choose.RegisterEvent("afterItemSelected",function(){..})
choose.unRegisterEvent("afterItemSelected");
*/
var LocationChoose = function (setting) {
    this.container = $("#" + setting.container);
    this.areaLevel = setting.areaLevel || 3;
    /* Css name  */
    this._classSelected = setting.classSelected || "areaselected";
    this._classNotSelected = setting.classNotSelected || "areanotselected";
    /* Event        
    this delegate accept two pamaters,
    NO.1:sz_code of the selected item;
    NO.2:name of the selected item;
    */
    this._afterItemSelected_ = setting.afterItemSelected;
    this.currentItem;
    /*province city country Regs */
    this._reg_province = /[1-9][0-9]0000/;
    this._reg_country = /[1-9][0-9]{3}[1-9]/;

    this.Init(setting.container, this.areaLevel);
}
LocationChoose.prototype.Init = function (container, level) {
    if (container) {
        this.showItems("", "", $("#" + container));
    } else {
        $("body").append("<div id='div-LocationChoose'></div>");
        this.container = $("#div-LocationChoose");
        this.showItems("", "", $("#div-LocationChoose"));
    }
}
//event tigger
LocationChoose.prototype.RegisterEvent = function (key, func) {
    if (key == "afterItemSelected")
        this._afterItemSelected_ = func;
}
//cancle event tigger
LocationChoose.prototype.UnRegisterEvent = function (key) {
    if (key == "afterItemSelected")
        this._afterItemSelected_ = null;
}
/*
show children-items under the right item;
name: item name;
szCode:item sz_code;
self.container: self.container by which children-items show;
*/
LocationChoose.prototype.showItems = function (name, szCode) {
    var self = this;
    var temp = [];
    //if szCode undefined then get sz_code by the right name
    var innerValue = szCode || this.GetItemByName(name);
    //get the liked items
    temp = self.GetChildrenItem(innerValue);

    if (temp.length > 0) {
        var result = "";
        for (var i = 0; i < temp.length; i++) {
            if (temp[i].name == name)
                result += "<div class='" + self._classSelected + "' title='" + temp[i].name + "' innerValue='" + temp[i].sz_code + "'>" + temp[i].name.substring(0, 4) + "</div>";
            else
                result += "<div class='" + self._classNotSelected + "' title='" + temp[i].name + "' innerValue='" + temp[i].sz_code + "'>" + temp[i].name.substring(0, 4) + "</div>";
        }
        //get the last item of temp,to judge what to show in the last position
        var last_sz_code = temp[temp.length - 1].sz_code;
        var last_sz_name = temp[temp.length - 1].name;

        if (/[1-9][0-9]{3}00/.test(last_sz_code) && !/[1-9][0-9]0000/.test(last_sz_code) || temp.length == 1)// if current  is city-page then back to native-page
            result += "<div class='areanotselected'>返回全国</div>";
        if (self._reg_country.test(last_sz_code))// if current  is country-page then back to province-page
            result += "<div class='areanotselected' szcode='" + last_sz_code.substr(0, 2) + "0000" + "' name='" + last_sz_name + "'>返回全省</div>";
        self.container.html(result);
        //$("#" + self.container + " div")
        $("div", self.container).each(function () {
            $(this).bind("click", function () {
                if ($(this).attr("innerText") == "返回全国") {
                    self.showItems('', "", self.container);
                }
                else if ($(this).attr("innerText") == "返回全省") {
                    self.showItems($(this).attr("name"), $(this).attr("szcode"), self.container);
                }
                else {
                    var sz_code = $(this).attr("innerValue");

                    $("div", self.container).removeClass("areaselected");
                    $("div", self.container).addClass("areanotselected");
                    $(this).addClass("areaselected");

                    // if the selected item hava children items,show ite's children items
                    if (/[\d]{2}0000/.test(sz_code) || (/[1-9][0-9]{3}00/.test(sz_code) && !/[1-9][0-9]0000/.test(sz_code)))
                        self.showItems($(this).attr("innerText"), sz_code, self.container)
                }
                if (self._afterItemSelected_)
                    self._afterItemSelected_($(this).attr("innerValue"), $(this).attr("title"));
            });
        });
    }
    else {
        self.container.html("出现故障，请稍候重试");
    }
}

LocationChoose.prototype.GetChildrenItem = function (innerValue) {
    var self = this; var temp = [];
    if (innerValue) {
        if (self._reg_province.test(innerValue)) {//sz_code like 省 then add city items
            for (i in citys)
                if (innerValue.substr(0, 2) == i.substr(0, 2))
                    temp.push({ sz_code: i, name: citys[i].name });
        }
        else if (/[1-9][0-9]{3}00/.test(innerValue) && !/[1-9][0-9]0000/.test(innerValue))//sz_code like 市 then add country items
        {
            for (i in countrys)
                if (innerValue.substr(0, 4) == i.substr(0, 4))
                    temp.push({ sz_code: i, name: countrys[i].name });
        }
        else if (self._reg_country.test(innerValue))  //sz_code like 县 then add country items
        {
            for (i in countrys)
                if (innerValue.substr(0, 4) == i.substr(0, 4))
                    temp.push({ sz_code: i, name: countrys[i].name });
        }

        if (temp.length == 0) {
            temp.push({ sz_code: innerValue, name: name });
        }
    }
    else {
        for (i in provinces)
            temp.push({ sz_code: i, name: provinces[i].name });
    }
    return temp;
}
LocationChoose.prototype.GetItemType = function (item_sz_code) {
    if (/[1-9][0-9]0000/.test(item_sz_code))
        return 1;
    else if (/[1-9][0-9]{3}00/.test(item_sz_code) && !/[1-9][0-9]0000/.test(item_sz_code))
        return 2;
    else if (/[1-9][0-9]{3}[1-9]/.test(item_sz_code))
        return 3;
    else
        return 0;
}
LocationChoose.prototype.GetItemByName = function (str) {
    if (str.replace(/(^\s*)|(\s*$)/g, "") != "") {
        if (provinces)
            for (i in provinces)//遍历省份
                if (provinces[i].name.indexOf(str) >= 0) {
                    return i;
                }
        if (citys)
            for (i in citys)//遍历市
                if (citys[i].name.indexOf(str) >= 0) {
                    return i;
                }
        if (countrys)
            for (i in countrys)//遍历县
                if (countrys[i].name.indexOf(str) >= 0) {
                    return i;
                }
    }
    return null;
}
LocationChoose.prototype.GetNameBySzCode = function (str) {
    if (str.replace(/(^\s*)|(\s*$)/g, "") != "") {

        return provinces[str] || citys[str] || countrys[str];
        //        if (provinces)
        //            for (i in provinces)//遍历省份
        //                if (i == str) {
        //                    return provinces[i];
        //                }
        //        if (citys)
        //            for (i in citys)//遍历市
        //                if (i == str) {
        //                    return citys[i];
        //                }
        //        if (countrys)
        //            for (i in countrys)//遍历县
        //                if (i == str) {
        //                    return countrys[i];
        //                }
    }
    return null;
}
